package cn.itcast.wanxinp2p.consumer.service.impl;

import cn.itcast.wanxinp2p.api.acount.model.AccountDTO;
import cn.itcast.wanxinp2p.api.acount.model.AccountRegisterDTO;
import cn.itcast.wanxinp2p.api.acount.model.ConsumerRegisterDTO;
import cn.itcast.wanxinp2p.api.service.ConsumerService;
import cn.itcast.wanxinp2p.common.domain.CodePrefixCode;
import cn.itcast.wanxinp2p.common.domain.RestResponse;
import cn.itcast.wanxinp2p.common.domain.StatusCode;
import cn.itcast.wanxinp2p.common.util.CodeNoUtil;
import cn.itcast.wanxinp2p.consumer.agent.AccountApiAgent;
import cn.itcast.wanxinp2p.consumer.common.ConsumerErrorCode;
import cn.itcast.wanxinp2p.consumer.common.exception.BusinessException;
import cn.itcast.wanxinp2p.consumer.entity.Consumer;
import cn.itcast.wanxinp2p.consumer.mapper.ConsumerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.dromara.hmily.annotation.Hmily;

@Slf4j
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    ConsumerMapper consumerMapper;

    @Autowired
    private AccountApiAgent accountApiAgent;

    @Override
    public Integer checkMobile(String mobile) {
        //根据手机号查询用户，存在返回1 不存在返回0
        return null;
    }

    @Override
    @Hmily(confirmMethod = "confirmRegister", cancelMethod = "cancelRegister")
    public void register(ConsumerRegisterDTO consumerRegisterDTO) {
        //1、检测是否已注册，如果已经注册，则抛出 ConsumerErrorCode.E_140107 异常
        Consumer consumer = consumerMapper.selectByMobile(consumerRegisterDTO.getMobile());
        //2、如果用户没有注册，则保存该用户数据 到 consumer 表中
        if (consumer != null) {
            throw new BusinessException(ConsumerErrorCode.E_140107);
        }
        consumer = new Consumer();
        BeanUtils.copyProperties(consumerRegisterDTO, consumer);
        consumer.setUsername(CodeNoUtil.getNo(CodePrefixCode.CODE_NO_PREFIX));
        consumer.setUserNo(CodeNoUtil.getNo(CodePrefixCode.CODE_REQUEST_PREFIX));
        consumer.setIsBindCard(0);
        consumerMapper.register(consumer);
        //3、并通过Feign远程调用 统一账号服务 实现 账号的保存
        AccountRegisterDTO accountRegisterDTO = new AccountRegisterDTO();
        BeanUtils.copyProperties(consumerRegisterDTO, accountRegisterDTO);
        RestResponse<AccountDTO> restResponse = accountApiAgent.register(accountRegisterDTO);
        if (restResponse.getResult() == null) {
            throw new BusinessException(ConsumerErrorCode.E_140106);
        }
    }

    public void confirmRegister(ConsumerRegisterDTO consumerRegisterDTO) {
        //用户已存在修改状态为1
        consumerMapper.updateByStatus(StatusCode.STATUS_IN.getCode(), consumerRegisterDTO.getMobile());
        log.info("-------------- 执行 confirmRegister");
    }

    public void cancelRegister(ConsumerRegisterDTO consumerRegisterDTO) {
        log.info("-------------- 执行 cancelRegister");
        //TODO 删除用户
        Consumer consumer = consumerMapper.selectByMobile(consumerRegisterDTO.getMobile());
        if (consumer == null) {
            return;
        }
        if (StatusCode.STATUS_IN.getCode() != consumer.getStatus()) {
            consumerMapper.deleteByMobile(consumerRegisterDTO.getMobile());
        }
    }
}